/*
 * @Author: Joel
 * @Date: 2025-04-10 16:52:07
 * @LastEditors: Joel
 * @LastEditTime: 2025-04-14 13:42:22
 * @FilePath: /gd32f303cct6/usr/board_driver/control.c
 * @Description: 
 * Copyright (C) 2025 YiJiaLink, All Rights Reserved. 
 * 2025-04-10 16:52:07
 */

#include"control.h"

static void control_gpio_init(void);
static void run_control(enum CTRL_OUTPUT ctrl,uint8_t flag);
static void stop_control(enum CTRL_OUTPUT ctrl);
static struct ctrl_func  ctrl;

/**
 * @func: 
 * @description: 初始化rled
 * @param {rled_func} rled_func
 * @return {*}
 * @example: 
 */
struct ctrl_func *ctrl_driver_init(void)
{
    struct ctrl_func *c_func = &ctrl;
    c_func->device_init = control_gpio_init;
    c_func->ctrl_run = run_control;
    c_func->ctrl_stop = stop_control;

    return c_func;
}
/**
 * @func: 
 * @description: 初始化GPIO口
 * @return {*}
 * @example: 
 */
static void control_gpio_init(void)
{
    /* enable the led clock */
    rcu_periph_clock_enable(CTRLA_RCU);
    rcu_periph_clock_enable(CTRLB_RCU);
    /* 复用GPIO */
    rcu_periph_clock_enable(RCU_AF);
    gpio_pin_remap_config(GPIO_SWJ_NONJTRST_REMAP, ENABLE);
    gpio_init(GPIOB, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
    gpio_bit_set(GPIOB, GPIO_PIN_4);
    /* configure led GPIO port */
    gpio_init(PWR_CTRL_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, PWR_CTRL_PIN);
    gpio_init(VALVE_CTRL_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, VALVE_CTRL_PIN);
    gpio_init(FSTCH_CTRL_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, FSTCH_CTRL_PIN);
    gpio_init(EMS_CTRL_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, EMS_CTRL_PIN);
    gpio_init(OLED_RES_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, OLED_RES_PIN);
    gpio_init(OLED_DC_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, OLED_DC_PIN);
    gpio_init(OLED_CS_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, OLED_CS_PIN);
    gpio_init(IR_CTRL_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, IR_CTRL_PIN);

    return;
}
/**
 * @func: 
 * @description: 输出电平控制
 * @param {enum CTRL_OUTPUT} ctrl
 * @param {uint8_t} flag
 * @return {*}
 * @example: 
 */
static void run_control(enum CTRL_OUTPUT ctrl,uint8_t flag)
{
    uint32_t gpiox, pin;
    switch (ctrl)
    {
    case PWR_CTRL:
        /* code */
        gpiox = PWR_CTRL_GPIO_PORT;
        pin = PWR_CTRL_PIN;
        break;
    case VALVE_CTRL:
        /* code */
        gpiox = VALVE_CTRL_GPIO_PORT;
        pin = VALVE_CTRL_PIN;
        break;
    case FSTCH_CTRL:
        /* code */
        gpiox = FSTCH_CTRL_GPIO_PORT;
        pin = FSTCH_CTRL_PIN;
        break;
    case EMS_CTRL:
        /* code */
        gpiox = EMS_CTRL_GPIO_PORT;
        pin = EMS_CTRL_PIN;
        break;
    case OLED_RES:
        /* code */
        gpiox = OLED_RES_GPIO_PORT;
        pin = OLED_RES_PIN;
        break;
    case OLED_DC:
        /* code */
        gpiox = OLED_DC_GPIO_PORT;
        pin = OLED_DC_PIN;
        break;
    case OLED_CS:
        /* code */
        gpiox = OLED_CS_GPIO_PORT;
        pin = OLED_CS_PIN;
        break;
    case IR_CTRL:
        /* code */
        gpiox = IR_CTRL_GPIO_PORT;
        pin = IR_CTRL_PIN;
        break;
    default:
        break;
    }

    if(RESET != flag){
        GPIO_BOP(gpiox) = pin;
    }else{
        GPIO_BC(gpiox) = pin;
    }
    return;
}
/**
 * @func: 
 * @description: 关闭控制
 * @return {*}
 * @example: 
 */
static void stop_control(enum CTRL_OUTPUT ctrl)
{
    uint32_t gpiox, pin;
    switch (ctrl)
    {
    case PWR_CTRL:
        /* code */
        gpiox = PWR_CTRL_GPIO_PORT;
        pin = PWR_CTRL_PIN;
        break;
    case VALVE_CTRL:
        /* code */
        gpiox = VALVE_CTRL_GPIO_PORT;
        pin = VALVE_CTRL_PIN;
        break;
    case FSTCH_CTRL:
        /* code */
        gpiox = FSTCH_CTRL_GPIO_PORT;
        pin = FSTCH_CTRL_PIN;
        break;
    case EMS_CTRL:
        /* code */
        gpiox = EMS_CTRL_GPIO_PORT;
        pin = EMS_CTRL_PIN;
        break;
    case OLED_RES:
        /* code */
        gpiox = OLED_RES_GPIO_PORT;
        pin = OLED_RES_PIN;
        break;
    case OLED_DC:
        /* code */
        gpiox = OLED_DC_GPIO_PORT;
        pin = OLED_DC_PIN;
        break;
    case OLED_CS:
        /* code */
        gpiox = OLED_CS_GPIO_PORT;
        pin = OLED_CS_PIN;
        break;
    case IR_CTRL:
        /* code */
        gpiox = IR_CTRL_GPIO_PORT;
        pin = IR_CTRL_PIN;
        break;
    default:
        break;
    }
    gpio_bit_reset(gpiox, pin);
    return;
}
